package com.clhbolg.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.clhbolg.annotation.AccessLimit;
import com.clhbolg.model.dto.AuroraHomeInfoDTO;
import com.clhbolg.model.dto.UserDetailsDTO;
import com.clhbolg.model.vo.request.SubscribeVO;
import com.clhbolg.model.vo.request.UserInfoVO;
import com.clhbolg.model.vo.request.UserLoginVo;
import com.clhbolg.model.vo.request.UserVO;
import com.clhbolg.service.IUserAuthService;
import com.clhbolg.service.IUserInfoService;
import com.clhbolg.service.WebInfoService;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.concurrent.ExecutionException;

/**
 * 用户前台操作接口
 *
 * @Author cailinhu
 * @Description
 * @Date 2023/2/28 10:36
 * @Version 1.0
 */
@RequestMapping("/users")
@RestController
public class UsersController {
    @Autowired
    IUserAuthService userAuthService;

    @Autowired
    IUserInfoService userInfoService;

    @Autowired
    WebInfoService webInfoService;

    /**
     * 用户登录
     * @param vo
     * @return
     */
    @PostMapping("/login")
    @SaIgnore
    public R login(UserLoginVo vo){
        UserDetailsDTO userDetailsDTO = userAuthService.loginBlogUser(vo);
        return R.ok(userDetailsDTO);
    }

    /**
     * 用户注册
     * @param vo
     * @return
     */
    @PostMapping("/register")
    @SaIgnore
    public R userRegister(@Valid @RequestBody UserVO vo){
        userAuthService.register(vo);
        return R.ok();
    }

    /**
     * 发送邮箱验证码
     * @param username
     * @return
     */
    @AccessLimit(second = 60,maxCount = 1)
    @GetMapping("/code")
    @SaIgnore
    public R sendCode(String username) {
        userAuthService.sendCode(username);
        return R.ok();
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    @SaIgnore
    public R loginOut(){
        StpUtil.logout();
        return R.ok("退出成功");
    }


    /*
      以下是涉及到多数据源的操作，为了保证事务有效，和成功切换数据源；统一用的userAuthService 做的入口。
     */

    /**
     * 更新用户信息
     * @param userInfoVO
     * @return
     */
    @PutMapping("/info")
    public R updateUserInfo(@Valid @RequestBody UserInfoVO userInfoVO){
        userAuthService.updateUserInfo(userInfoVO);
        return R.ok();
    }

    /**
     * 修改用户头像
     * @param file
     * @return
     */
    @PostMapping("/avatar")
    @SaCheckRole("admin")
    public R updateAvatar(MultipartFile file){
        String s = userInfoService.updateAvatar(file);
        return R.ok(R.message,s);
    }

    /**
     * 修改用户的订阅状态
     * @param subscribeVO
     * @return
     */
    @PutMapping("/subscribe")
    public R subscribe(SubscribeVO subscribeVO){
        userInfoService.updateUserSubscribe(subscribeVO);
        return R.ok();
    }

    /**
     * 返回网站的一些基本信息
     *
     * @return
     */
    @GetMapping("/websiteConfig")
    public R getWebsiteConfig() throws ExecutionException, InterruptedException {
        AuroraHomeInfoDTO webInfoConfig = webInfoService.getWebInfoConfig();
        return R.ok(webInfoConfig);
    }
}
